﻿using Autofac;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using WebTest.Core.Services;
using WebTest.Infrastructure.Data;
using WebTest.Infrastructure.NLog;

namespace WebTest.WebApi.Configuration
{
    public class ConfigureAutofac : Autofac.Module
    {
        private readonly IConfiguration configuration;
        public ConfigureAutofac(IConfiguration configuration)
        {
            this.configuration = configuration ?? throw new ArgumentNullException(nameof(configuration));
        }
        protected override void Load(ContainerBuilder builder)
        {
            #region Infrasturcture
            // context
            builder.Register(c =>
            {
                var options = new DbContextOptionsBuilder<OrderContext>();
                //options.UseMySql(configuration.GetConnectionString("MySql"), new MySqlServerVersion(new Version(10, 1, 40)), mysqlOptions =>
                //{
                //    mysqlOptions.EnableRetryOnFailure(maxRetryCount: 15, maxRetryDelay: TimeSpan.FromSeconds(30), errorNumbersToAdd: null);
                //});
                options.UseMySql("Server=192.168.1.233;port=3306;database=test;uid=test01;pwd=test123456;pooling=true;CharSet=utf8mb4;SslMode=None");
                return options;
            }).InstancePerDependency();
            builder.RegisterType<OrderContext>().AsSelf().InstancePerLifetimeScope();

            #endregion

            #region Core
            builder.RegisterType<MemberSignService>()
                .AsImplementedInterfaces()
                .InstancePerDependency();

            builder.RegisterType<OrderService>()
                .AsImplementedInterfaces()
                .InstancePerDependency();
            #endregion

            #region Application
            builder.RegisterType<NLogService>()
                .AsImplementedInterfaces()
                .SingleInstance();
            #endregion

            builder.RegisterAssemblyTypes(Assembly.GetExecutingAssembly());
        }
    }
}
